package org.easy.tencsms;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 类描述：
 *
 * @date 2022-03-30
 */
@Configuration
@EnableConfigurationProperties({QCloudsmsProperties.class})
@AutoConfigureAfter
@Slf4j
public class QCloudsmsAutoConfiguration implements InitializingBean {
    private final QCloudsmsProperties qCloudsmsProperties;

    public QCloudsmsAutoConfiguration(QCloudsmsProperties qCloudsmsProperties) {
        this.qCloudsmsProperties = qCloudsmsProperties;
    }

    @Bean
    @ConditionalOnMissingBean
    public QCloudsmsService qcloudsmsService() {
        return new QCloudsmsServiceImpl(qCloudsmsProperties.getAppid(), qCloudsmsProperties.getKey());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        checkParam();
    }

    private void checkParam() {
        if (qCloudsmsProperties.getAppid() == null ||
                qCloudsmsProperties.getKey() == null) {
            log.error("未配置" + QCloudsmsProperties.class.getTypeName());
        }

    }
}
